//
//  SelectView.m
//  Mahjong
//
//  Created by Chen on 16/9/2.
//  Copyright © 2016年 Elliot. All rights reserved.
//

#import "SelectView.h"
#import "SelectTitleView.h"
#import "SynthesizeView.h"

@interface SelectView ()<SelectTitleViewDelegate>
{
    UIButton *zonghe;
    UIButton *zhjiao;
     UIButton *jiage;
    UIButton *jgjiao;
  UIButton *xiaoliang;

}
/** 综合 */
@property (nonatomic , strong) SelectTitleView *synthesizeTitleView;
/** 价格 */
@property (nonatomic , strong) SelectTitleView *priceTitleView;
/** 销量 */
@property (nonatomic , strong) SelectTitleView *salesTitleView;

/** changeBtn */
@property (nonatomic , strong) UIButton *changeBtn;



@end

@implementation SelectView

- (instancetype)initSelectViewWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubviews];
    }
    return self;
}

- (void)createSubviews{
    CGFloat width = (kUIScreenWidth - 60) / 3.0;
    
    zonghe = [UIButton buttonWithType:UIButtonTypeCustom];
    zonghe.frame = CGRectMake(0, 0, width, self.frame.size.height);
    [zonghe setTitle:@"综合" forState:UIControlStateNormal];
    [zonghe setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [self addSubview:zonghe];
    [zonghe addTarget:self action:@selector(zongAction:) forControlEvents:UIControlEventTouchUpInside];
    
    zhjiao = [UIButton buttonWithType:UIButtonTypeCustom];
    zhjiao.frame = CGRectMake(width-30, 10, 20, 15);
    [zhjiao setImage:[UIImage imageNamed:@"icon_arow_up@2x"] forState:UIControlStateNormal];
    [self addSubview:zhjiao];
    
    jiage = [UIButton buttonWithType:UIButtonTypeCustom];
    jiage.frame = CGRectMake(width, 0, width, self.frame.size.height);
    [jiage setTitle:@"价格" forState:UIControlStateNormal];
    [jiage setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self addSubview:jiage];
    [jiage addTarget:self action:@selector(jiaAction:) forControlEvents:UIControlEventTouchUpInside];

    jgjiao = [UIButton buttonWithType:UIButtonTypeCustom];
    jgjiao.frame = CGRectMake(width*2-30, 10, 20, 15);
    [jgjiao setImage:[UIImage imageNamed:@"icon_hui_up@2x"] forState:UIControlStateNormal];
    [self addSubview:jgjiao];
    
    xiaoliang = [UIButton buttonWithType:UIButtonTypeCustom];
    xiaoliang.frame = CGRectMake(2 * width, 0, width, self.frame.size.height);
    [xiaoliang setTitle:@"销量" forState:UIControlStateNormal];
    [xiaoliang setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self addSubview:xiaoliang];
    [xiaoliang addTarget:self action:@selector(xiaoAction:) forControlEvents:UIControlEventTouchUpInside];

    
//    self.synthesizeTitleView = [SelectTitleView initSelectTitleViewWithFrame:CGRectMake(0, 0, width, self.frame.size.height)];
//    self.synthesizeTitleView.titleLabel.text = @"综合";
//    self.synthesizeTitleView.delegate = self;
//    [self addSubview:self.synthesizeTitleView];
    
//    self.priceTitleView = [SelectTitleView initSelectTitleViewWithFrame:CGRectMake(width, 0, width, self.frame.size.height)];
//    self.priceTitleView.titleLabel.text = @"价格";
//    self.priceTitleView.delegate = self;
//    [self addSubview:self.priceTitleView];
//    
//    self.salesTitleView = [SelectTitleView initSelectTitleViewWithFrame:CGRectMake(2 * width, 0, width, self.frame.size.height)];
//    self.salesTitleView.titleLabel.text = @"销量";
//    self.salesTitleView.delegate = self;
//    [self addSubview:self.salesTitleView];
    
    self.changeBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.changeBtn.frame = CGRectMake(3 * width +15 , 5 , 30, self.frame.size.height - 10);
    [self.changeBtn setBackgroundImage:[UIImage imageNamed:@"comlist_but_rank"] forState:(UIControlStateNormal)];
    [self.changeBtn setBackgroundImage:[UIImage imageNamed:@"comlist_but_ranktwo"] forState:(UIControlStateSelected)];
    [self.changeBtn addTarget:self action:@selector(changeMode:) forControlEvents:(UIControlEventTouchUpInside)];
    [self addSubview:self.changeBtn];
    
    UIView *bottomLineView = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height-1, self.frame.size.width, 1)];
    bottomLineView.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:bottomLineView];
    
}

- (void)changeMode:(UIButton *)btn{
    btn.selected = !btn.selected;
    if (self.delegate && [self.delegate respondsToSelector:@selector(selectView:changeBtn:)]) {
        [self.delegate selectView:self changeBtn:btn];
    }
}
-(void)zongAction:(UIButton *)btn{
    btn.selected = !btn.selected;
    [zonghe setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [jiage setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [xiaoliang setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [jgjiao setImage:[UIImage imageNamed:@"icon_hui_up@2x"] forState:UIControlStateNormal];

    if (btn.selected) {
        [zhjiao setImage:[UIImage imageNamed:@"icon_arow_down@2x"] forState:UIControlStateNormal];

        [self.delegate zonghe:1];
    }
    if (!btn.selected) {
        [zhjiao setImage:[UIImage imageNamed:@"icon_arow_up@2x"] forState:UIControlStateNormal];

        [self.delegate zonghe:0];
    }
    
    

}
-(void)jiaAction:(UIButton *)btn{
    btn.selected = !btn.selected;
    if (btn.selected) {
       [jgjiao setImage:[UIImage imageNamed:@"icon_arow_down@2x"] forState:UIControlStateNormal];
    }
    if (!btn.selected) {
         [jgjiao setImage:[UIImage imageNamed:@"icon_arow_up@2x"] forState:UIControlStateNormal];
    }
     [zhjiao setImage:[UIImage imageNamed:@"icon_hui_up@2x"] forState:UIControlStateNormal];
    [zonghe setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [jiage setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [xiaoliang setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.delegate jiage];

}
-(void)xiaoAction:(UIButton *)btn{
    [zhjiao setImage:[UIImage imageNamed:@"icon_hui_up@2x"] forState:UIControlStateNormal];
    [jgjiao setImage:[UIImage imageNamed:@"icon_hui_up@2x"] forState:UIControlStateNormal];

    [zonghe setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [jiage setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [xiaoliang setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [self.delegate xiaoliang];

}

#pragma mark -- SelectTitleViewDelegate
- (void)clickSelectTitleView:(SelectTitleView *)selectTitleView{

//    if (selectTitleView == self.synthesizeTitleView) {
////        [self.delegate zonghe];
////        SynthesizeView *synthesizeView = [SynthesizeView initSynthesizeView];
////        [self.superview addSubview:synthesizeView];
////        synthesizeView.delegate = (UIViewController <SynthesizeViewDelegate>*)self.inputViewController;
//    }
//    if (selectTitleView == self.priceTitleView) {
//        [self.delegate jiage];
////        SynthesizeView *synthesizeView = [SynthesizeView initSynthesizeView];
////        [self.superview addSubview:synthesizeView];
////        synthesizeView.delegate = (UIViewController <SynthesizeViewDelegate>*)self.inputViewController;
//    }
//    if (selectTitleView == self.salesTitleView) {
//        [self.delegate xiaoliang];
////        SynthesizeView *synthesizeView = [SynthesizeView initSynthesizeView];
////        [self.superview addSubview:synthesizeView];
////        synthesizeView.delegate = (UIViewController <SynthesizeViewDelegate>*)self.inputViewController;
//    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
